<?php session_start();
include "constants.php";
$dates=date_create(CUTOFFS);
$cutoffdates=date_format($dates,"m/d/Y");
$INITIALS=SPOINT;

$newip = $_SERVER['REMOTE_ADDR'];
$Employee=$_SESSION['Username'];


if(!isset($Employee)||empty($Employee)||$newip!= $_SESSION['IP'])
{echo "If you are payday loan applicant, please start your ". "<a href='../apply.php'>application here</a> now, thank you for your cooperation!";
exit();
}


$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD); 

if (!$link) { 
    die('Could not connect: ' . mysql_error()); 
} 
mysql_select_db(DB_NAME);


$mstatus =$items=$profiles=0; //declare vars
$nameditems=null;

if(isset($_POST["mstatus"]) && is_numeric($_POST["mstatus"]))
{
    $mstatus = $_POST["mstatus"];
}

if(isset($_POST["items"]) && is_numeric($_POST["items"]))
{
    $items = $_POST["items"];
}

if(isset($_POST["nameditems"]))
{
    $nameditems = $_POST["nameditems"];
	if($items==1){
		$profiles=$nameditems;
	}
	else if($items==2){
		$sql = "SELECT * FROM Applications WHERE AccountID='$nameditems'";
		$accounts = mysql_query($sql,$link);    
		$rows = mysql_fetch_array($accounts);
		$profiles=$rows['ProfileID'];
	}
	else if($items==3){
		$sql = "SELECT * FROM Profiles WHERE Email='$nameditems'";
		$accounts = mysql_query($sql,$link);    
		$rows = mysql_fetch_array($accounts);
		$profiles=$rows['ProfileID'];	
		$FNAME=$rows['FirstName'];	
		$LNAME=$rows['LastName'];	
		$REASONS=$rows['Reasons'];	

	}
}

/*if(isset($_POST["profiles"]) && is_numeric($_POST["profiles"]))
{
    $profiles = $_POST["profiles"];*/
    $sql = "SELECT * FROM Profiles WHERE ProfileID='$profiles'";
    $accounts = mysql_query($sql,$link);    
    $rows = mysql_fetch_array($accounts);
    $FNAME=$rows['FirstName'];
    $LNAME=$rows['LastName'];
    $Emails=$rows['Email'];
    $CITYS=$rows['City'];
    $PROVINCES=$rows['Province'];
    $COMMENTS=$rows['COMMENTS'];


    $sql = "SELECT * FROM Applications WHERE ProfileID='$profiles' ORDER BY AccountID DESC LIMIT 1";
    $accounts = mysql_query($sql,$link);    
    $rows = mysql_fetch_array($accounts);
    $BankName=$rows['BankName'];

//}


?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Cash2u.ca/Cashhub.ca: Employee login page</title>
<META NAME="Keywords" CONTENT="Payday loans|Online Payday Loans|Payday Loans Provider|Canadian Payday Loans|Faxless Payday Loans|Fast Payday Loans|cash advance|Payday Loans for Canadians|Easy Payday Loans and Cash Advances">
<META NAME="Description" CONTENT="Online Payday Loan Provider|Online Cash Advance Provider|Canadian Payday Loans|Payday Loans for Canadians|Fast Payday Loans|Instant Payday Loans|Faxless Payday Loans|Cash Advance">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="main_style.css"/>
<link rel="stylesheet" type="text/css" href="index.css"/>
<script src="../css/reportitems.js" type="text/javascript"></script> 
<script type="text/javascript" src="../css/usernamecheck.js"></script>
<style type="text/css">@import "../css/jquery.datepick.css";</style>
<script type="text/javascript" src="../css/jquery.datepick.js"></script>
<script type="text/javascript">
$(function() {
	$('#startmonth').datepick({yearRange: '1900:+1'});
	$('#endmonth').datepick({yearRange: '1900:+1'});	
	

});
function showDate(date) {
	alert('The date chosen is ' + date);
}
</script>

<script type="text/javascript">
function autoSubmit()
{
    var formObject = document.forms['myForm'];
    formObject.submit();
}

</script>


</head>
<body style="background-color:white;">
<br/><br/><br/><br/>
<div style="width: 740px;color:grey;" align="right">
<!--table cellspacing="4" cellpadding="1" border="0" style="margin:5px;height:50px;width:740px;background-color:#084c8d;border:1px solid #e5e5e5;color:white;font-size:110%;font-weight:bold;"-->
<table cellspacing="4" cellpadding="1" border="0" style="margin: 0 0 0 0;width:740px;background-color:yellow;">
<tr><td width="20"></td>
<td width="110" align="center"><a href="newapplication.php" style="color:grey;">Application</a></td>
<td width="110" align="center"><a href="management.php" style="color:grey;">Management</a></td>
<td width="110" align="center"><a href="reports.php" style="color:grey;">Report</a></td>
<td width="110" align="center"><a href="searches.php" style="color:grey;">Search</a></td>
<td width="110" align="center"><a href="logout.php" style="color:grey;">Logout</a></td>
<td width="90" align="center"></td>
<td width="90" align="center"></td>
<td width="140"></td>
</tr>
</table>
</div>
<br/>
<br/>

<form name='myForm' method="post">
<table cellspacing="4" cellpadding="1" border="0" style="margin: 0 0 0 0;">
<tr><td width="700" align="center">Report Items</td></tr>
<tr><td width="700" align="center">    
    <select style="width: 220px;" name="mstatus" id="mstatus" onChange="autoSubmit();">
	<option value="0" <?php if($mstatus == 0) echo " selected"; ?>>---report items----</option>
	<option value="1" <?php if($mstatus == 1) echo " selected"; ?>>Monthly Report</option>
	<option value="2" <?php if($mstatus == 2) echo " selected"; ?>>Annual Report</option>
	<option value="3" <?php if($mstatus == 3) echo " selected"; ?>>Applicant Report</option>
	<option value="4" <?php if($mstatus == 4) echo " selected"; ?>>Approved Report</option>
    </select></td></tr>

</table>
<?php if($mstatus == 1 || $mstatus == 2 || $mstatus == 4)  {    ?>
<table cellspacing="4" cellpadding="1" border="0" style="margin: 0 0 0 0;">
	<tr>
	<td width="150" align="right">Starting Date:</td>
        <td width="150"><input type="text" style="width: 100px;" name="startmonth" id="startmonth"  style="width: 150px;border:1px solid #4195fc;"></td>
        <td width="150" align="right">Ending Date:</td>
	<td width="150"><input type="text" style="width: 150px;border:1px solid #4195fc;" name="endmonth" id="endmonth"></td>
	</tr>		
</table>
<?php }    ?>
<?php if($mstatus == 3)  {    ?>
	<table cellspacing="4" cellpadding="1" border="0" style="margin: 0 0 0 0;">
		<tr><td width="700" align="center">Please Select Item to Search</td></tr>
		<tr><td width="700" align="center">  
		<select style="width: 220px;" name="items" onChange="autoSubmit();">        
		    	<option value="0" <?php if($items == 0) echo " selected"; ?>>----Item---</option>        
		    	<option value="1" <?php if($items == 1) echo " selected"; ?>>ProfileID</option>        
		    	<option value="2" <?php if($items == 2) echo " selected"; ?>>AccountID</option>        
		    	<option value="3" <?php if($items == 3) echo " selected"; ?>>Email Address</option>        
		</select>
		</td></tr>	
	</table>
	<?php if($items != 0)  {    ?>
		<table cellspacing="4" cellpadding="1" border="0" style="margin: 0 0 0 0;">
			<tr><td width="700" align="center">Please Enter <?php if($items==1) {echo "ProfileID";} else if($items==2) {echo "AccountID";} else if($items==3) {echo "Email";} ?> to search</td></tr>
			<tr><td width="700" align="center"><input type="text" name="nameditems" onChange="autoSubmit();" value="<?php echo $nameditems;?>"></td></tr>
		</table>
	<?php }    ?>

<!--table cellspacing="4" cellpadding="1" border="0" style="margin: 0 0 0 0;">
<tr><td width="700" align="center">Please Select Applicant</td></tr>
<tr><td width="700" align="center">  
    <select style="width: 220px;" name="profiles" onChange="autoSubmit();">        
    	<option value="0">------Applicant--------</option>        
        <?php                
        $sql = "SELECT DISTINCT AA.ProfileID AS ProfileID, PP.FirstName as FNAME, PP.LastName as LNAME,PP.EMAIL as EMAIL FROM Applications AA LEFT JOIN Profiles PP ON AA.ProfileID=PP.ProfileID 
		WHERE NewApp!='5' AND Datediff(SignDate , STR_TO_DATE('$cutoffdates' , '%m/%d/%Y' ))>0 ORDER BY ProfileID";
        $accounts = mysql_query($sql,$link);        
        while($row = mysql_fetch_array($accounts)) {        
            echo ("<option value=\"$row[ProfileID]\" " . ($profiles == $row["ProfileID"] ? " selected" : "") . ">$row[ProfileID]-$row[FNAME] $row[LNAME]-$row[EMAIL]</option>");  }  ?>
        
    </select>
</td></tr>	
</table-->
<?php if($profiles!=0) { ?>
<table cellspacing="4" cellpadding="1" border="1" style="margin: 0 0 0 0;">
	<tr style="background-color:grey;"><td width="90" align="right">First Name</td><td width="90" align="right">Last Name</td>
	<td width="90" align="right">City</td><td width="120" align="right">Province</td>
	<td width="100" align="right">Bank Name</td><td width="100" align="right">Email</td></tr>
	<tr style="background-color:yellow;"><td width="90" align="right"><?php echo $FNAME;?></td><td width="90" align="right"><?php echo $LNAME;?></td>
	<td width="90" align="right"><?php echo $CITYS;?></td><td width="120" align="right"><?php echo $PROVINCES;?></td>
	<td width="100" align="right"><?php echo $BankName;?></td><td width="100" align="right"><?php echo $Emails;?></td></tr>

</table></br>
<table cellspacing="4" cellpadding="1" border="1" style="margin: 0 0 0 0;">
	<tr style="background-color:grey;"><td width="70" align="right">AccountID</td><td width="50" align="right">Loan</td>
	<td width="70" align="right">Loan Cost</td><td width="90" align="right">NSF Amount</td>
	<td width="80" align="right">NSF Charge</td>
	<td width="90" align="right">Expected Pay</td><td width="80" align="right">Due Day</td>
	<td width="90" align="right">Paid Amount</td><td width="90" align="right">Comment</td></tr>
	<?php                
        $sql = "SELECT AA.AccountID as AccountID,AA.ApprovalAmount as ApprovalAmount,AA.COST_100 as COST_100,
		HN.NSFAMOUNT as NSFAMOUNT,HN.NSFCHARGE AS NSFCHARGE,
		CASE NewAmount WHEN 0 THEN AA.ApprovalAmount*(1+0.01*AA.COST_100) ELSE 
		(CASE HN.REASON WHEN -1 THEN AA.ApprovalAmount*(1+0.01*AA.COST_100) ELSE HN.EXPAMOUNT END) END AS EXPPAY,
		CASE NewAmount WHEN 0 THEN AA.CollectDate ELSE 
		(CASE HN.REASON WHEN -1 THEN AA.CollectDate ELSE HN.DUEDAY END) END AS CollectDate,
		CASE NewAmount WHEN 0 THEN AA.PaidAmount ELSE 
		(CASE HN.REASON WHEN -1 THEN 0 ELSE HN.PAID END) END AS PaidAmount,
		CASE NewAmount WHEN 0 THEN 0 ELSE 
		(CASE HN.REASON WHEN -1 THEN 'First NSF' ELSE HN.REASON END) END AS REASON		
		FROM Applications AA LEFT JOIN HISTNSF HN ON AA.AccountID=HN.AID WHERE ProfileID='$profiles' AND Datediff(SignDate , STR_TO_DATE('$cutoffdates' , '%m/%d/%Y' ))>0 ORDER BY AccountID,HID";
        $accounts = mysql_query($sql,$link);        
        while($row = mysql_fetch_array($accounts)) {  		    
	   echo "<tr><td width='70' align='right'>";
	   echo $row['AccountID']-$INITIALS;
	   echo "</td><td width='50' align='right'>";
	   echo $row['ApprovalAmount'];
	   echo "</td><td width='70' align='right'>";
	   echo $row['COST_100']*0.01*$row['ApprovalAmount'];

	   echo "</td><td width='90' align='right'>";
	   echo $row['NSFAMOUNT'];
	   echo "</td><td width='80' align='right'>";
	   echo $row['NSFCHARGE'];

	   echo "</td><td width='90' align='right'>";
	   echo $row['EXPPAY'];
	   echo "</td><td width='80' align='right'>";
	   echo $row['CollectDate'];
	   echo "</td><td width='90' align='right'>";
	   echo $row['PaidAmount'];

	   echo "</td><td width='90' align='right'>";
	   echo $row['REASON'];

	   echo "</td></tr>"; 
	}  ?>        
</table>
	<?php if($items == 3)  {    ?>
	<br/><br/>
	<table cellspacing="4" cellpadding="1" border="1" style="margin: 0 0 0 0;">
		<tr style="background-color:grey;"><td width="150" align="right">First Name</td><td width="150" align="right">Last Name</td>
		<td width="200" align="right">Failed Reason</td></tr>
		<tr style="background-color:yellow;"><td width="150" align="right"><?php echo $FNAME;?></td><td width="150" align="right"><?php echo $LNAME;?></td>
		<td width="200" align="right"><?php echo $REASONS;?></td></tr>
	</table>
	<?php } ?>
<?php } ?>


<?php }    ?>

<?php if($mstatus != 3)  {    ?>
<table cellspacing="4" cellpadding="1" border="0" style="margin: 0 0 0 0;">

<tr><td width="700" align="center"><input type='button' onclick='ajaxFunction()' 
                              value='View Report'/></td></tr>
</table>
<?php }    ?>
</form>
<div id='ajaxDiv'></div>


<?php if($mstatus == 3)  {    ?>
<form name='addcomment' method="post"  action="addcomments.php" target="_blank">
<table><!--to add or delete comments----->
<tr>
<td width="140" align="right">Comments:</td>
<td width="170"><textarea rows="5" cols="50" name="newcomments" value=""><?php echo $COMMENTS;?></textarea></td>   
</tr>
</table><!--comments--------------->
<table><!--to add or delete comments----->
<tr>
<td width="140" align="right"><input type="text" style="visibility:hidden;" name="add_profiles" value="<?php echo $profiles;?>"></td>
<td width="140" align="right"><input type="text" style="visibility:hidden;" name="add_status" value="<?php echo $mstatus;?>"></td>
</tr>
</table><!--comments--------------->

<table>	
<tr><td width="700" align="center"><input type="submit" value="Update Comments"/></td>
</tr>
</table>	

</form>
<?php }    ?>

<br/><br/>
<div style="width: 600px;color:grey;" align="right">
<b><strong>Copyright &#169 2011 All rights reserved</strong></b>
</div>



</body>
</html>